/**
 * 
 */
package com.wsd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.wsd.bean.Message;
import com.wsd.bean.ProductOrder;
import com.wsd.bean.Users;
import com.wsd.service.UserService;
import com.wsd.util.PagingBuilder;

/**
 * @author TuZiJun
 *
 */
public class UserAction extends ActionSupport {
	private Users user = null;
	private Message message = null;
	private UserService userService = null;
	private int page = 1;
	private static final int PAGESIZE = 10;
	public void setMessage(Message message) {
		this.message = message;
	}
	/**
	 * @return the message
	 */
	public Message getMessage() {
		return message;
	}

	public void setUser(Users user) {
		this.user = user;
	}
	public Users getUser() {
		return user;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public String registe() {
		boolean result = userService.saveUser(user);
		HttpServletRequest request = ServletActionContext.getRequest();
		if(!result) {
			request.setAttribute("info", "注册失败！请检查信息是否输入正确。");
			return INPUT;
		}
		return SUCCESS;
	}
	public String goRegiste() {
		return SUCCESS;
	}
	public String login(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Users u = userService.login(user);
		if (null == u) {
			request.setAttribute("message", "用户名或密码错误！");
			return LOGIN;
		}
		request.getSession().setAttribute("user", u);
		return SUCCESS;
	}
	
	public String checkName() {
//		name = URLDecoder.decode(name, "UTF-8");
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(userService.checkName(user)){
        	out.write("该用户名已存在！");
        }else{
        	out.write("该用户名可用！");
        }
		return NONE;
	}
	
	public String logout(){
		ServletActionContext.getRequest().getSession().removeAttribute("user");
		return SUCCESS;
	}
	public String liveMessage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		Users u = (Users) session.getAttribute("user");
		if (null == u)
			return LOGIN;
		if (!u.isState()) {
			request.setAttribute(ERROR, "您的账号当前正在审核中。。。");
			return SUCCESS;
		}
		boolean result = userService.saveMessage(message);
		if(!result)
			request.setAttribute(ERROR, "留言失败！！！");
		return showMessage();
	}
	public String showMessage(){
		HttpServletRequest request = ServletActionContext.getRequest();
		List<Message> messages = userService.showMessage(page, PAGESIZE);
		int max = userService.getMessagePage(PAGESIZE);
		request.setAttribute("messages", messages);
		int pagerange = max > 10 ? 10 : max;
		request.setAttribute("pageuri", PagingBuilder.build("/wsd/showMessage.action?page=@page@", page, pagerange, max));
		return SUCCESS;
	}
	public String showOrder() {
		HttpServletRequest request = ServletActionContext.getRequest();
		Users u = (Users) request.getSession().getAttribute("user");
		List<ProductOrder> orders = userService.showOrder(u.getUserid());
		request.setAttribute("orders", orders);
		return SUCCESS;
	}
}
